#include <bits/stdc++.h>
using namespace std;
bool st;
namespace Fastio {
const int L = 1 << 20;
inline char gc() {
    static char buf[L], *l = buf, *r = buf;
    if (l == r)
        r = (l = buf) + fread(buf, 1, L, stdin);
    return (l == r) ? EOF : *l++;
}
char obuf[L], *p = obuf;
inline void pc(char c) {
    if (p == obuf + L)
        fwrite(obuf, 1, L, stdout), p = obuf;
    *p++ = c;
}
inline void flush() { fwrite(obuf, 1, p - obuf, stdout); }
struct Reader {
    template <typename T>
    inline Reader& operator>>(T& x) {
        x = 0;
        short f = 1;
        char c = gc();
        while (!isdigit(c)) {
            if (c == '-')
                f = -1;
            c = gc();
        }
        while (isdigit(c)) x = 10 * x + (c - '0'), c = gc();
        x *= f;
        return *this;
    }
    Reader() {}
} cin;
struct Writer {
    template <typename T>
    inline Writer& operator<<(T x) {
        if (x < 0)
            pc('-'), x = -x;
        static short tp = 0, s[40];
        do
            s[++tp] = x % 10, x /= 10;
        while (x);
        while (tp) pc(s[tp--] + '0');
		return *this;
    }
    inline Writer& operator<<(const char* s) {
        int i = 0;
        while (s[i]) pc(s[i++]);
        return *this;
    }
    inline Writer& operator<<(char c) {
        pc(c);
        return *this;
    }
    Writer() {}
    ~Writer() { flush(); }
} cout;
#define cin Fastio::cin
#define cout Fastio::cout
}
int n, q, hson[200005], dep[200005], dfn[200005], id[200005], fa[200005], sz[200005], top[200005], rt[200005], dft;
long long dst[200005];
vector<pair<int, int>> edges[200005];

inline void dfs1(int i) {
	sz[i] = 1;
	for (auto j : edges[i]) {
		if (j.first == fa[i]) continue;
		fa[j.first] = i; dep[j.first] = dep[i] + 1;
		dst[j.first] = dst[i] + j.second;
		dfs1(j.first);
		sz[i] += sz[j.first];
		if (sz[j.first] > sz[hson[i]]) hson[i] = j.first;
	}
}

inline void dfs2(int i, int tp) {
	dfn[i] = ++dft; id[dft] = i; top[i] = tp;
	if (hson[i]) dfs2(hson[i], tp);
	for (auto j : edges[i]) {
		if (j.first == fa[i] || j.first == hson[i]) continue;
		dfs2(j.first, j.first);
	}
}

inline int get_lca(int x, int y) {
	if (!x || !y) return x | y;
	if (x == 1) return x;
	if (dfn[x] <= dfn[y] && dfn[y] <= dfn[x] + sz[x] - 1) return x;
	if (dfn[y] <= dfn[x] && dfn[x] <= dfn[y] + sz[y] - 1) return y;
	while (top[x] != top[y]) {
		if (dep[top[x]] < dep[top[y]]) swap(x, y);
		x = fa[top[x]];
	}
	if (dep[x] < dep[y]) return x;
	return y;
}

inline int get_k(int x, int k) {
	while (k >= dep[x] - dep[top[x]] + 1) {
		k -= dep[x] - dep[top[x]] + 1;
		x = fa[top[x]];
	}
	return id[dfn[x] - k];
}

namespace SGT {
	struct Info {
		int lca, res, ls, rs;
	} t[200005*18*15];
	int tot = 0;
	inline void update(int &i, int pre, int l, int r, int qp) {
		if (l > qp || r < qp) return;
		t[i = ++tot] = t[pre];
		if (l == r) {
			t[i].lca = qp;
			t[i].res = 1;
			return;
		}
		int mid = (l + r) / 2;
		update(t[i].ls, t[pre].ls, l, mid, qp);
		update(t[i].rs, t[pre].rs, mid + 1, r, qp);
		t[i].lca = get_lca(t[t[i].ls].lca, t[t[i].rs].lca);
		t[i].res = t[t[i].ls].res + t[t[i].rs].res;
	}
	int x, y;
	inline void query(int i, int l, int r, int ql, int qr, int typ = 0) {
		if (typ && y) return;
		if (l >= ql && r <= qr) {
			if (x != 1) x = get_lca(x, t[i].lca);
			y += t[i].res;
			return;
		}
		int mid = (l + r) / 2;
		if (t[i].ls && mid >= ql) query(t[i].ls, l, mid, ql, qr, typ);
		if (t[i].rs && mid < qr && !(typ && y)) query(t[i].rs, mid + 1, r, ql, qr, typ);
	}
}

inline void dfs3(int i) {
	if (hson[i]) {
		dfs3(hson[i]);
		SGT::update(rt[i], rt[hson[i]], 1, n, i);
	} else {
		SGT::update(rt[i], 0, 1, n, i);
		return;
	}
	for (auto j : edges[i]) {
		if (j.first == fa[i] || j.first == hson[i]) continue;
		for (int it = dfn[j.first]; it <= dfn[j.first] + sz[j.first] - 1; ++it) {
			int tmp = 0;
			SGT::update(tmp, rt[i], 1, n, id[it]);
			rt[i] = tmp;
		}
		dfs3(j.first);
	}
}
bool ed;
signed main() {
	freopen("memory.in", "r", stdin); freopen("memory.out", "w", stdout);
	cin >> n >> q;
	mt19937 rnd(time(0));
	for (int i = 1; i < n; ++i) {
		int u = i + 1, v = max(1, i - (int)(rnd() % 5)), w = i * 3;
		cin >> u >> v >> w;
		edges[u].push_back({v, w});
		edges[v].push_back({u, w});
	}
	dfs1(1);
	dfs2(1, 1);
	dfs3(1);
	long long lst = 0;
	for (int i = 1; i <= q; ++i) {
		long long p = i%n+1, l = rnd() % n + 1, r = rnd() % n + 1;
		cin >> p >> l >> r;
		p ^= lst; l ^= lst; r ^= lst;
		if (l > r) swap(l, r);
		SGT::x = SGT::y = 0; SGT::query(rt[p], 1, n, l, r);
		if (SGT::y == r - l + 1) {
			lst = dst[SGT::x] - dst[p];
			cout << lst << '\n';
			continue;
		} else if (SGT::y != 0) {
			lst = 0;
			cout << lst << '\n';
			continue;
		}
		int L = -1, R = dep[p], nowx = 0, lstp = p;
		while (L + 1 != R) {
			int mid = (L + R) / 2, it = get_k(lstp, mid - nowx);
			SGT::x = 1; SGT::y = 0; SGT::query(rt[it], 1, n, l, r, 1);
			if (SGT::y) R = mid;
			else {
				L = mid;
				lstp = it;
				nowx = mid;
			}
		}
		int it = get_k(p, R);
		SGT::x = SGT::y = 0; SGT::query(rt[it], 1, n, l, r);
		if (SGT::y) {
			if (SGT::y == r - l + 1) {
				lst = dst[p] + dst[SGT::x] - 2 * dst[it];
			} else lst = dst[p] - dst[it];
			cout << lst << '\n';
		} else assert(0);
	}
	return 0;
}

